<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>新增订单</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body { background: #f5f7fa; font-family: 'Microsoft YaHei', Arial, sans-serif; margin: 0; }
        .main-card {
            max-width: 700px;
            margin: 48px auto 0 auto;
            background: #fff;
            border-radius: 18px;
            box-shadow: 0 4px 24px rgba(0,0,0,0.08);
            padding: 48px 0 40px 0;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .main-title {
            font-size: 36px;
            font-weight: 800;
            color: #222;
            margin-bottom: 38px;
            align-self: center;
            margin-left: 0;
        }
        .form-row {
            display: flex;
            align-items: center;
            margin-bottom: 32px;
            width: 80%;
            justify-content: center;
        }
        .form-row label {
            width: 110px;
            font-size: 20px;
            color: #333;
            font-weight: 500;
            text-align: right;
            margin-right: 24px;
        }
        .form-row input, .form-row select {
            flex: 1;
            padding: 16px 18px;
            border: none;
            border-radius: 10px;
            background: #f5f7fa;
            font-size: 18px;
            color: #222;
            outline: none;
            transition: box-shadow 0.2s;
        }
        .form-row input:focus, .form-row select:focus {
            background: #fff;
            box-shadow: 0 0 0 2px #1890ff33;
        }
        .form-actions {
            width: 80%;
            display: flex;
            justify-content: flex-end;
            gap: 18px;
            margin-top: 18px;
        }
        .form-actions button {
            padding: 12px 38px;
            border: none;
            border-radius: 8px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: background 0.2s;
        }
        .form-actions .submit-btn { background: #13c2c2; color: #fff; }
        .form-actions .submit-btn:hover { background: #1890ff; }
        .form-actions .reset-btn { background: #fff; color: #1890ff; border: 2px solid #1890ff; }
        .form-actions .reset-btn:hover { background: #e6f7ff; }
        .form-actions .back-btn { background: #aaa; color: #fff; }
        .form-actions .back-btn:hover { background: #888; }
        @media (max-width: 800px) {
            .main-card { max-width: 98vw; padding: 18px 0 18px 0; }
            .main-title { margin-left: 18px; font-size: 26px; }
            .form-row label { width: 80px; font-size: 16px; }
            .form-row input, .form-row select { font-size: 15px; }
            .form-actions button { padding: 8px 18px; font-size: 15px; }
        }
    </style>
</head>
<body>
<div class="main-card">
    <div class="main-title" id="formTitle">新增订单</div>
    <form id="orderForm" autocomplete="off" style="width:100%;">
        <div class="form-row">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        </div>
        <div class="form-row">
            <label for="location">所属地</label>
            <select id="location" name="location" required>
                <option value="">请选择所属地</option>
            </select>
        </div>
        <div class="form-row">
            <label for="product">商品名</label>
            <select id="product" name="product" required>
                <option value="">请选择商品名</option>
            </select>
        </div>
        <div class="form-row">
            <label for="orderDate">购买日期</label>
            <input type="date" id="orderDate" name="orderDate" placeholder="yyyy/mm/dd" required>
        </div>
        <div class="form-row">
            <label for="quantity">购买数量</label>
            <input type="number" id="quantity" name="quantity" placeholder="请输入购买数量" required min="1">
        </div>
        <div class="form-actions">
            <button type="submit" class="submit-btn">提交</button>
            <button type="reset" class="reset-btn">重置</button>
            <button type="button" class="back-btn" onclick="window.location.href='manage.html'">返回主页</button>
        </div>
    </form>
</div>
<script>
// 获取URL参数
function getQueryParam(name) {
    var match = window.location.search.match(new RegExp('(?:[?&])' + name + '=([^&]*)'));
    return match ? decodeURIComponent(match[1]) : null;
}
// 动态加载景点名字到所属地下拉
fetch('/api/scenic', { method: 'GET' })
    .then(response => response.json())
    .then(data => {
        var select = document.getElementById('location');
        if (Array.isArray(data) && data.length > 0) {
            data.forEach(function(item) {
                var opt = document.createElement('option');
                opt.value = item.name;
                opt.text = item.name;
                select.appendChild(opt);
            });
        }
    });
var orderId = getQueryParam('id');
if (orderId) {
    document.getElementById('formTitle').innerText = '编辑订单';
    window.addEventListener('DOMContentLoaded', function() {
        fetch('/api/order/' + orderId)
            .then(response => response.json())
            .then(item => {
                document.getElementById('username').value = item.username || '';
                document.getElementById('product').value = item.product || '';
                document.getElementById('location').value = item.location || '';
                if (item.orderDate) {
                    var dt = item.orderDate.split('T')[0];
                    document.getElementById('orderDate').value = dt;
                }
                document.getElementById('quantity').value = item.quantity || '';
            });
    });
}
document.getElementById('orderForm').onsubmit = function(e) {
    e.preventDefault();
    var username = document.getElementById('username').value.trim();
    var product = document.getElementById('product').value.trim();
    var location = document.getElementById('location').value;
    var orderDate = document.getElementById('orderDate').value;
    var quantity = document.getElementById('quantity').value;
    // 校验商品名前两字和所属地一致
    if (!product.startsWith(location.slice(0,2))) {
        alert('商品名前两字必须与所属地一致！');
        return;
    }
    var order = {
        username: username,
        product: product,
        location: location,
        orderDate: orderDate,
        quantity: quantity
    };
    var url = '/api/order' + (orderId ? ('/' + orderId) : '');
    var method = orderId ? 'PUT' : 'POST';
    if (orderId) order.id = orderId;
    fetch(url, {
        method: method,
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(order)
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('操作成功');
            window.location.href = 'manage.html';
        } else {
            alert(data.message || '操作失败');
        }
    });
};

// 假设有一个商品数据源
const productData = {
    '黄山': ['黄山门票', '黄山索道票', '黄山一日游'],
    '厦门大学': ['厦门大学门票', '厦门大学讲解服务'],
    '南京': ['南京门票', '南京博物院票'],
    // 可继续补充
};

// 监听所属地变化，自动联想商品名
const locationSelect = document.getElementById('location');
const productSelect = document.getElementById('product');

function updateProductOptions() {
    const location = locationSelect.value;
    productSelect.innerHTML = '<option value="">请选择商品名</option>';
    if (productData[location]) {
        productData[location].forEach(item => {
            const opt = document.createElement('option');
            opt.value = item;
            opt.text = item;
            productSelect.appendChild(opt);
        });
    }
}
locationSelect.addEventListener('change', updateProductOptions);

// 页面加载时也初始化一次
updateProductOptions();
</script>
</body>
</html> 